<?php
require_once( './core/Common/config.inc.php' );
require_once( './core/Common/common.php' );
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_USER.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

//require_once( '../system/Class_ERROR.php' );
//require_once( '../system/login.inc.php' );
//require_once( '../system/ecjp_m.php' );
//require_once( '../system/Class_PWD.php' );
if(isset($_GET)){
$forms = $_GET;
}

$dao = new Class_M_USER;

$wherearr =  array();

if( isset( $forms['free_word'] ) &&  ( !empty( $forms['free_word'] ) ) ){
	$wherearr[] = sprintf("(a.workshop_name like  '%s' or 
	                       a.user_name1 like '%s' or 
	                       a.user_name2 like '%s' or
	                       a.tel_number like '%s' or
	                       a.email like '%s' )",
	                       '%'.$dao->esc( $forms['free_word'] ).'%',
	                       '%'.$dao->esc( $forms['free_word'] ).'%',
	                       '%'.$dao->esc( $forms['free_word'] ).'%',
	                       '%'.$dao->esc( $forms['free_word'] ).'%',
	                       '%'.$dao->esc( $forms['free_word'] ).'%');

}

if ( isset( $forms['local_cd'] ) &&  ( !empty( $forms['local_cd'] ) ) ){
	$wherearr[] = sprintf(" a.local_name =  '%s' ",  $dao->esc( $forms['local_cd'] ) );
}


if ( isset( $forms['state'] ) &&  ( !empty( $forms['state'] ) ) ){
	$statekey = $forms['state'];
	$key='(';
	for($i=0;$i<count($statekey);$i++)
	{
		if($i!=count($statekey)-1){
		  
		  $key.= $statekey[$i].',';	
		}else {
		  $key.= $statekey[$i];
		} 
		if($statekey[$i] == 1)
		{$smarty->assign('state2',1);}
	    if($statekey[$i] == 2)
		{$smarty->assign('state1',2);}
	    if($statekey[$i] == 3)
		{$smarty->assign('state3',3);}
	}
	$key.=')' ;	
	$wherearr[] = sprintf(" a.state in  %s ",   $dao->esc($key));	
}

if ( isset( $forms['send'] ) &&  ( !empty( $forms['send'] ) ) ){
    $sendkey = $forms['send'];
	$key='(';
	for($i=0;$i<count($sendkey);$i++)
	{
		if($i!=count($sendkey)-1){
		  
		  $key.= $sendkey[$i].',';	
		}else {
		  $key.= $sendkey[$i];
		} 
		if($sendkey[$i] == 1)
		{$smarty->assign('send1',1);}
	    if($sendkey[$i] == 2)
		{$smarty->assign('send2',2);}
	}
	$key.=')' ;	
	$wherearr[] = sprintf(" a.send in  %s ",   $dao->esc($key));	
}

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "user".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	//header('Content-type: text/csv; charset=UTF-16LE');
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	

	$colums = array();
	/*
	 *  login_id	            ログインID
		login_pwd 	            パスワード
		user_name	           ユーザー名
		workshop_name   貴医院名・貴技工所名
		zipcode         郵便番号
		street_name     市区町村
		email	                      メールアドレス
		tel_number      電話番号
		status           状態

	 */
	 //1 string code 2 integer 3 date 4 string ""必要 
	$col_heads = array(
		'login_id'      => array( 'ログインID',     1),
		'login_pwd'     => array( 'パスワード',     1),
		'user_name'     => array( 'ユーザー名',     1),
		'workshop_name' => array( '貴医院名・貴技工所名',1),
		'zipcode'       => array( '郵便番号',1),
		'street_name'     => array( '市区町村', 1),
		'email'         => array( 'メールアドレス',1),
		'tel_number'   => array( '電話番号', 1),
		'status '       => array( '状態',1),
	);
	

	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}

	

	$data = $dao->search_csv( $wherearr );
	//データ整形開始
	foreach ($data as $k => $v) {
		//$colums["type"][$k] = $v[1];
		$data[$k]["user_name"]      =  $v['user_name1'].' '.$v['user_name2'];

	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];	
}else{
	$page['current'] = 1;
}
$page['item'] = PAGE_MAX;

if( isset($forms) ) $smarty->assign('forms', $forms);



$dao_other = new Class_M_OTHER;

$data = $dao->search($wherearr,$page);
$dao_local = $dao_other->get_local_array('都道府県');


if( isset($dao_local)) $smarty->assign('dao_local', $dao_local);
if( isset($data) ) $smarty->assign('data', $data);
if( isset($page) )  $smarty->assign('page', $page);
$subUrl = U('Home/User/subSearch');
$smarty->assign('subUrl', $subUrl);

$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('user_index.html');
?>